//
//  ResetPasswdViewController.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/22.
//  Copyright © 2017年 why. All rights reserved.
//


#import "ResetPasswdViewController.h"
@interface ResetPasswdViewController ()

@property (nonatomic, strong) UITextField * oldpassword;
@property (nonatomic, strong) UITextField * newpassword;

@end

@implementation ResetPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(240, 240, 240);

    [self addTopView];
    [self createUI];
}

#pragma mark - --------------添加头视图----------------
- (void)addTopView{
    UIView * topView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(375), ActualH(64))];
        CAGradientLayer * gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = view.bounds;
        //设置渐变区域的起始和终止位置（范围为0-1）
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        //设置颜色数组
        gradientLayer.colors = @[(__bridge id)RGB(22, 125, 255).CGColor, (__bridge id)RGB(0, 178, 255).CGColor];
        //设置颜色分割点（范围：0-1）
        gradientLayer.locations = @[@(0.0f), @(1.0f)];
        [view.layer addSublayer:gradientLayer];
        view;
    });
    [self.view addSubview:topView];
    
    UILabel * title = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(152.5), ActualH(32), ActualW(72), ActualH(17))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"修改密码";
        label.font = FontX(FONTSIZE(17));
        label;
    });
    [topView addSubview:title];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(31.5), ActualW(18), ActualH(18))];
    imageView.image = [UIImage imageNamed:@"back"];
    [topView addSubview:imageView];
    
    
    UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(13.5), ActualW(41), ActualH(44))];
    [backButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    
    [topView addSubview:backButton];
    
}

- (void)createUI {
    NSArray * titles = @[@"原密码",@"新密码"];
    for (int i = 0; i < 2; i++) {
        UIView * breakView = ({
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(64) + ActualH(55) * i, ActualW(375), ActualH(10))];
            view.backgroundColor = RGB(240, 240, 240);
            view;
        });
        [self.view addSubview:breakView];
        
        UIView * backView = ({
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(74) + ActualH(55) * i, ActualW(375), ActualH(45))];
            view.backgroundColor = RGB(255, 255, 255);
            view;
        });
        [self.view addSubview:backView];
        
        UILabel * name = ({
            UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(15.5), ActualW(43.5), ActualH(14))];
            label.textColor = RGB(72, 72, 72);
            label.text = titles[i];
            label.backgroundColor = RGB(255, 255, 255);
            label.textAlignment = NSTextAlignmentLeft;
            label.font = FontX(FONTSIZE(13));
            label;
        });
        [backView addSubview:name];
        
        if (i == 0) {
            self.oldpassword = [[UITextField alloc] initWithFrame:CGRectMake(ActualW(100.5), ActualH(15.5), ActualW(275), ActualH(14))];
            self.oldpassword.secureTextEntry = YES;
            self.oldpassword.font = FontX(FONTSIZE(13));
            self.oldpassword.placeholder = @"请输入密码";
            [backView addSubview:self.oldpassword];
        }else {
            self.newpassword = [[UITextField alloc] initWithFrame:CGRectMake(ActualW(100.5), ActualH(15.5), ActualW(275), ActualH(14))];
            self.newpassword.secureTextEntry = YES;
            self.newpassword.font = FontX(FONTSIZE(13));
            self.newpassword.placeholder = @"请输入新密码";
            [backView addSubview:self.newpassword];
        }
    }
    
    UILabel * desc = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(185), ActualW(355), ActualH(13))];
        label.textColor = RGB(102, 102, 102);
        label.text = @"密码由6-20位英文字母、数字或符号组成";
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = FontX(FONTSIZE(12));
        label;
    });
    [self.view addSubview:desc];
    
    UIButton * action = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(206), ActualW(355), ActualH(40))];
        [button addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = FontX(FONTSIZE(18));
        [button setTitle:@"提交" forState:UIControlStateNormal];
        [button setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
        button.backgroundColor = RGB(64, 137, 248);
        button.layer.cornerRadius = ActualW(4);
        button;
    });
    [self.view addSubview:action];
}

- (void)submit {
    
    [[URLFather sharedInstance] exchangePasswordWithOld:self.oldpassword.text new:self.newpassword.text success:^(NSDictionary *result) {

        if ([Pub resultSuccess:result]) {

            kShowSuccess(@"修改成功");
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            kShowText(result[@"msg"])
        }
    } failure:^(NSError *error) {
        kShowError(@"网络出错");
    }];
}


- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
